package com.heima.controller;

import com.heima.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 获取秒杀路径
     */
    @GetMapping("/path/{userId}/{goodsId}")
    public String getSeckillPath(@PathVariable Long userId, @PathVariable Long goodsId) {
        // 实际应该验证用户身份
        return seckillService.createPath(userId, goodsId);
    }

    /**
     * 执行秒杀操作
     */
    @PostMapping("/{path}/do_seckill")
    public String doSeckill(@PathVariable String path,
                            @RequestParam Long userId,
                            @RequestParam Long goodsId) {
        // 验证路径
        boolean isValid = seckillService.checkPath(userId, goodsId, path);
        if (!isValid) {
            return "路径无效";
        }

        // 执行秒杀
        return seckillService.executeSeckill(userId, goodsId);
    }

    /**
     * 获取秒杀结果
     */
    @GetMapping("/result")
    public String seckillResult(@RequestParam Long userId, @RequestParam Long goodsId) {
        return seckillService.getSeckillResult(userId, goodsId);
    }
}